import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Author quan
 * @Description 实现一个简单的Servlet代码:dengyue
 * @Date 2023/6/21 22:20
 */

//2、注册请求的路由：通过注解实现。就能感知到/hello了。
@WebServlet("/hello")
//1、继承HttpServlet
public class Demo1_HelloServlet extends HttpServlet {
    //3、重写doGet或者doPost方法
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端给的name参数
        String name = req.getParameter("name");
        //设置编码格式：支持中文
        //方式1:不成功
//        resp.setCharacterEncoding("utf8");
        //方式2
        resp.setContentType("text/html;charset=utf-8");
        //将结果返回给前端
        resp.getWriter().println("姓名="+name);
    }
    //4、重写doPost方法
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将请求转发到doGet方法中
        this.doGet(req,resp);
    }
}
